import { usePublicHooks } from "../../../../../hooks";
import { reactive, onMounted, ref, toRaw } from "vue";
import { StatisticsEntry } from "@/api/parking_record/statistics";
import { useRouter } from "vue-router";

export function parknflyStaticParkEntryConfig() {
  const router = useRouter();

  // 获取当前日期
  const currentDate = new Date();

  // 获取本月第一天
  const firstDayOfMonth = new Date(
    currentDate.getFullYear(),
    currentDate.getMonth(),
    1
  );
  const formattedFirstDay = formatDate(firstDayOfMonth);

  function formatDate(date) {
    const year = date.getFullYear();
    const month = (date.getMonth() + 1).toString().padStart(2, "0");
    const day = date.getDate().toString().padStart(2, "0");
    const hours = date.getHours().toString().padStart(2, "0");
    const minutes = date.getMinutes().toString().padStart(2, "0");
    const seconds = date.getSeconds().toString().padStart(2, "0");
    return (
      year +
      "-" +
      month +
      "-" +
      day +
      " " +
      hours +
      ":" +
      minutes +
      ":" +
      seconds
    );
  }

  // 获取本月最后一天
  const lastDayOfMonth = new Date(
    currentDate.getFullYear(),
    currentDate.getMonth() + 1,
    0
  );
  const formattedLastDay = formatDate(lastDayOfMonth);

  const form = reactive({
    in_time: [formattedFirstDay, formattedLastDay],
    order_type: 2,
    page: 1,
    limit: 20
  });

  //从父级钩子里取东西
  const { loadingConfig, dataList, loading } = usePublicHooks();

  const pagination = ref({ current: 1, pageSize: 30, total: 0 });

  async function onSearch() {
    loading.value = true;
    const { data } = await StatisticsEntry(toRaw(form));
    dataList.value = data.list;
    pagination.value = {
      ...pagination.value,
      total: data.list.length
    };
    setTimeout(() => {
      loading.value = false;
    }, 500);
  }

  //页面加载
  onMounted(() => {
    onSearch();
  });

  //重置表单
  function resetForm(formEl) {
    if (!formEl) return;
    formEl.resetFields();
    onSearch();
  }

  const onPageSizeChange = (size: number) => {
    form.limit = size;
    onSearch();
  };
  const onCurrentChange = (current: number) => {
    form.page = current;
    onSearch();
  };

  function jump(date) {
    router.push({
      path: "/parking_record/parknfly/order/list",
      query: { in_time: date }
    });
  }

  return {
    form,
    loading,
    onSearch,
    resetForm,
    dataList,
    pagination,
    loadingConfig,
    onPageSizeChange,
    onCurrentChange,
    jump
  };
}
